﻿function AzureStorageApi(credentials, azureChannel) {

    var self = this;

    var channel = azureChannel || new AzureProxyChannel();

    var signature = new AzureSignature(credentials);

    function onError(request) {
        var responeText = request.responseText;
    }

    function execute(request, options) {
        request.sign(credentials);

        return channel.send(request, function (data, req) {
            if (options && options.success) {
                if (request.acceptBase64) {
                    data = new Base64().decode(data);
                }
                options.success(data, req);
            }
        }, function (data, req) {
            onError(req);
            var action = 'notify';
            if (options && options.error) action = options.error(data, req);
            if (action == 'notify') {
                notificationArea.showError(req.statusText);
            }
        }, function () {
        });
    }

    var tables = new AzureStorageTablesApi(credentials, channel);

    var blobs = new AzureStorageBlobsApi(credentials, channel);
    
    var queues = new AzureStorageQueuesApi(credentials, channel);

    self.tables = tables;
    self.blobs = blobs;
    self.queues = queues;

    self.blob = blobs;

    self.getCredentials = function () { return credentials; };
    self.signature = signature;
    self.execute = execute;
}
